package com.cman777.springc.swagger.config;

import com.cman777.springc.swagger.properties.Swagger2Properties;
import com.google.common.base.Predicates;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @Author: chenzhicong
 * @Date: 2019/11/07 19:43
 * @Version 1.0
 */
@Configuration
@EnableSwagger2
@Log4j2
@EnableConfigurationProperties(Swagger2Properties.class)
public class Swagger2Config {

    @Autowired
    private Swagger2Properties swagger2Properties;

    @Bean("czc")
    public Docket customApis() {
        log.info("初始化swagger2Config");
        //=====添加head参数start============================
        List<Parameter> pars = new ArrayList();
        if(!CollectionUtils.isEmpty(swagger2Properties.getHeaderParam())){
            swagger2Properties.getHeaderParam().forEach((k, v)->{
                ParameterBuilder tokenPar = new ParameterBuilder();
                Parameter parameter = tokenPar.name(k)
                        .modelRef(new ModelRef(v))
                        .parameterType("header")
                        .required(false).build();
                pars.add(parameter);
            });
        }
        // =========添加head参数end===================

        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                // 错误路径不监控
                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                .build()
                .apiInfo(apiInfo())
                .directModelSubstitute(LocalDateTime.class, Date.class)
                .directModelSubstitute(LocalDate.class, String.class)
                .directModelSubstitute(LocalTime.class, String.class)
                .directModelSubstitute(ZonedDateTime.class, String.class)
                .globalOperationParameters(pars)
                ;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(Optional.ofNullable(swagger2Properties.getTitle()).orElse(""))
                .description(Optional.ofNullable(swagger2Properties.getDescription()).orElse(""))
                .version(Optional.ofNullable(swagger2Properties.getVersion()).orElse(""))
                .build();
    }

}
